﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class pauseScript : MonoBehaviour
{
    public GameObject pauseMenu;
    public GameObject Settings;
    public GameObject Slider;

    public AudioSource AudioSource;
    private float volumeRocker = 1f;

    // Start is called before the first frame update
    void Start()
    {
      //The pause menu canvas will be turned off by default so that 
      //the player doesn't start with the pause menu.
      pauseMenu.SetActive(false);

      AudioSource.Play();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            if(!pauseMenu.activeInHierarchy)
            {
                pauseTheGame();
            }

            else if(pauseMenu.activeInHierarchy)
            {
                returnTogame();
            }
        }


        //Adapt to the music slider
        musicGame();
    }

    public void pauseTheGame()
    {
        // Pause the game when Esc is pressed.
        Time.timeScale = 0f;
        pauseMenu.SetActive(true);
        Settings.SetActive(true);
        Slider.SetActive(false);
    }

    public void returnTogame()
    {
        // The game will resume if Return button is pressed OR
        // if the user instead decided to press Esc again.
        Time.timeScale = 1f;
        pauseMenu.SetActive(false);
    }

    public void moveToSettings()
    {
        Settings.SetActive(false);
        Slider.SetActive(true);
    }

    // The default volume data is provided to the volumeRocker variable.
    void musicGame()
    {
        AudioSource.volume = volumeRocker;
    }

    /*
        This method, which is frequently updated, is attached to the volume slider.
        As the user adjusts the volume slider, the data from volumeRocker
        is set to "volume".
    */
    public void updateVolume(float volume)
    {
        volumeRocker = volume;
    }
}
